//
//  YYMusicListViewController.m
//  音乐播放器
//
//  Created by zpy on 15/11/27.
//  Copyright © 2015年 zpy. All rights reserved.
//

#import "YYMusicListViewController.h"
#import "YYMusic.h"
#import "YYMusicTool.h"
#import "UIImage+Circle.h"
#import "YYPlayingViewController.h"

@interface YYMusicListViewController ()
// 整在播放的控制器
@property(nonatomic,strong) YYPlayingViewController *playingVc;
//@property(nonatomic,strong) NSArray *musics;
@end

@implementation YYMusicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 80;
    
}
#pragma mark - 懒加载歌曲信息数据模型
//-(NSArray *)musics{
//    if (_musics == nil) {
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"Musics.plist" ofType:nil];
//        NSArray *array = [NSArray arrayWithContentsOfFile:path];
//        NSMutableArray *mArray = [[NSMutableArray alloc] init];
//        for (NSDictionary *dict in array) {
//            YYMusic *music = [[YYMusic alloc] initWithDict:dict];
//            [mArray addObject:music];
//        }
//        _musics = mArray;
//    }
//    if (_musics == nil) {
//        self.musics = [YYMusic objectArrayWithFilename:@"Musics.plist"];
//    }
//    return _musics;
//}
// 创建播放控制器
-(YYPlayingViewController *) playingVc {

    if (_playingVc == nil) {
        _playingVc = [[YYPlayingViewController alloc] init];
    }
    return _playingVc;
}
#pragma mark - 数据源方法
// 选中某个cell 跳转到播放界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 去掉cell  的选中状态
   // [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YYMusic *music = [YYMusicTool musics][indexPath.row];
    
    [YYMusicTool setPlayingMusic:music];
    
    [self.playingVc show];
    
}

// 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [YYMusicTool musics].count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ID = @"musicCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    // 取出模型对象
    YYMusic *music = [YYMusicTool musics][indexPath.row];
    // 给cell赋值
    cell.imageView.image = [UIImage circleImageWithName:music.singerIcon borderWidth:2.0 borderColor:[UIColor greenColor]];
    cell.textLabel.text = music.name;
    cell.detailTextLabel.text = music.singer;
    
    return cell;
}
@end
